<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form
        :model="where"
        label-width="50px"
        class="ele-form-search"
        @keyup.enter.native="reload"
        @submit.native.prevent>
        <el-row :gutter="15">
          <el-col :lg="3" :md="12">
              <el-form-item label="年度:">
                <el-select
                  clearable
                  v-model="where.yearly"
                  placeholder="">
                  <el-option v-for="item in yearList" :key="item" :label="item" :value="item" />
                </el-select>
              </el-form-item>
            </el-col>
          <el-col :lg="3" :md="12">
            <el-form-item label="月份:">
              <el-select
                clearable
                v-model="where.monthly"
                placeholder="">
                <el-option value="1" label="1"></el-option>
                <el-option value="2" label="2"></el-option>
                <el-option value="3" label="3"></el-option>
                <el-option value="4" label="4"></el-option>
                <el-option value="5" label="5"></el-option>
                <el-option value="6" label="6"></el-option>
                <el-option value="7" label="7"></el-option>
                <el-option value="8" label="8"></el-option>
                <el-option value="9" label="9"></el-option>
                <el-option value="10" label="10"></el-option>
                <el-option value="11" label="11"></el-option>
                <el-option value="12" label="12"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
            <el-col :lg="4" :md="12">
              <el-form-item label="物料编码:" label-width="75px">
                <el-input
                  clearable
                  v-model="where.materialCode"
                  placeholder=""/>
              </el-form-item>
            </el-col>
            <el-col :lg="3" :md="12">
              <el-form-item label="采购方:" label-width="60px">
                <el-input
                  clearable
                  v-model="where.purchaseCode"
                  placeholder=""/>
              </el-form-item>
            </el-col>
            <el-col :lg="3" :md="12">
              <el-form-item label="销售方:" label-width="60px">
                <el-input
                  clearable
                  v-model="where.saleCode"
                  placeholder=""/>
              </el-form-item>
            </el-col>

            <el-col :lg="4" :md="12">
              <el-form-item label="同步状态: " label-width="75px">
                <el-select
                clearable
                v-model="where.synStatus"
                placeholder="请选择状态"
                class="ele-fluid">
                <el-option label="未同步" value="N"/>
                <el-option label="已同步" value="Y"/>
              </el-select>
              </el-form-item>
            </el-col>
              <el-col :lg="4" :md="12">
            <div class="ele-form-actions">
              <el-button
                type="primary"
                icon="el-icon-search"
                class="ele-btn-icon"
                size="small"
                @click="reload">查询
              </el-button>
              <el-button @click="reset" size="small">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :where="where"
        :datasource="url"
        :columns="columns"
        :selection.sync="selection"
        height="calc(100vh - 315px)">
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button
            @click="exportData"
            type="primary"
            icon="el-icon-download"
            class="ele-btn-icon"
            size="small"
            v-if="permission.includes('sys:loginlog:export')">导出
          </el-button>
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit(null)"
            v-if="permission.includes('sys:commercialacquisitionreport:add1')">添加
          </el-button>
          <el-button
            size="small"
            type="danger"
            icon="el-icon-delete"
            class="ele-btn-icon"
            @click="removeBatch"
            v-if="permission.includes('sys:commercialacquisitionreport:dall1')">删除
          </el-button>
        </template>
        <!-- 操作列 -->
        <template slot="action" slot-scope="{row}" v-if="false">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit(row)"
            v-if="permission.includes('sys:commercialacquisitionreport:edit')">修改
          </el-link>
          <el-popconfirm
            class="ele-action"
            title="确定要删除此收购报表吗？"
            @confirm="remove(row)">
            <el-link
              type="danger"
              slot="reference"
              :underline="false"
              icon="el-icon-delete"
              v-if="permission.includes('sys:commercialacquisitionreport:delete')">删除
            </el-link>
          </el-popconfirm>
        </template>
      </ele-pro-table>
    </el-card>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import XLSX from 'xlsx';
export default {
  name: 'ExamCommercialAcquisitionReport',
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      // 表格数据接口
      url: '/commercialacquisitionreport/index',
      // 表格列配置
      columns: [
        {
          columnKey: 'selection',
          type: 'selection',
          width: 45,
          align: 'center',
          fixed: "left"
        },
        {
          prop: 'id',
          label: 'ID',
          width: 60,
          align: 'center',
          showOverflowTooltip: true,
          fixed: "left"
        },
        {
          prop: 'yearly',
          label: '年度',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'monthly',
          label: '月度',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'purchaseCode',
          label: '采购方编码',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'purchaseName',
          label: '采购方名称',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'saleCode',
          label: '销售方编码',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'saleName',
          label: '销售方名称',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'materialCode',
          label: '物料编码',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'materialName',
          label: '物料名称',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'purchaseNum',
          label: '采购量',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'purchaseAmount',
          label: '采购额',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'purchaseMargin',
          label: '采购边际',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'twoVoteNum',
          label: '两票制数量',
          showOverflowTooltip: true,
          minWidth: 120,
          align: 'center'
        },
        {
          prop: 'twoVoteMoney',
          label: '两票制金额',
          showOverflowTooltip: true,
          minWidth: 120,
          align: 'center'
        },
        {
          prop: 'notTwoVoteNum',
          label: '非两票制数量',
          showOverflowTooltip: true,
          minWidth: 130,
          align: 'center'
        },
        {
          prop: 'notTwoVoteMoney',
          label: '非两票制金额',
          showOverflowTooltip: true,
          minWidth: 130,
          align: 'center'
        },
        {
          prop: 'dataSource',
          label: '数据来源',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'bipId',
          label: 'BIP主键',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center'
        },
        {
            prop: 'synStatus',
            label: '同步状态',
            showOverflowTooltip: true,
            minWidth: 100,
            align: 'center',
            formatter: (row, column, cellValue) => {
            return this.statusFormat(row, column,cellValue);
            }
          },
        // {
        //   prop: 'creator',
        //   label: '创建人',
        //   showOverflowTooltip: true,
        //   minWidth: 100,
        //   align: 'center'
        // },
        // {
        //   prop: 'modifier',
        //   label: '修改人',
        //   showOverflowTooltip: true,
        //   minWidth: 100,
        //   align: 'center'
        // },
        // {
        //   prop: 'modifyTime',
        //   label: '修改时间',
        //   sortable: 'custom',
        //   showOverflowTooltip: true,
        //   minWidth: 160,
        //   align: 'center',
        //   formatter: (row, column, cellValue) => {
        //     return this.$util.toDateString(cellValue);
        //   }
        // },
        {
          prop: 'createTime',
          label: '创建时间',
          sortable: 'custom',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center',
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        },
        // {
        //   columnKey: 'action',
        //   label: '操作',
        //   width: 150,
        //   align: 'center',
        //   resizable: false,
        //   slot: 'action',
        //   fixed: "right"
        // }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      yearList:[],
    };
  },
  mounted() {
    this.getYearList();
  },
  methods: {
    statusFormat(row, column,cellValue) {
        const daterc = row[column.property]
        if(daterc !=null){
            return cellValue=="Y"?"已同步":"未同步";
            }
    },
    /* 导出数据 */
    exportData() {
      let array = [['ID', '年度', '月度', '采购方编码','采购方名称', '销售方编码','销售方名称', '物料编码','物料名称', '采购量', '采购额', '采购边际','两票制数量' ,'两票制金额','非两票制数量','非两票制金额','数据来源', 'BIP主键', '同步状态']];
      const loading = this.$loading({lock: true});
      let url = "/commercialacquisitionreport/index?page=1&limit=20000000";
      let param = "";
      Object.keys(this.where).forEach(key=>{
        if(this.where[key] && this.where[key]!="")
        {
          param+="&"+key+"="+this.where[key];
        }
      });
      url = url+param;
      this.$http.get(url).then(res => {
        loading.close();
        if (res.data.code === 0) {
          res.data.data.records.forEach(d => {
            array.push([
              d.id,
              d.yearly,
              d.monthly,
              d.purchaseCode,
              d.purchaseName,
              d.saleCode,
              d.saleName,
              d.materialCode,
              d.materialName,
              d.purchaseNum,
              d.purchaseAmount,
              d.purchaseMargin,
              d.twoVoteNum,
              d.twoVoteMoney,
              d.notTwoVoteNum,
              d.notTwoVoteMoney,
              d.datasource,
              d.bipId,
              d.synStatus
            ]);
          });
          this.$util.exportSheet(XLSX, array, '收购报表');
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    getYearList()
    {
      this.$http.get('/commercialacquisitionreport/yearlist').then(res => {
        if (res.data.code === 0) {
          this.yearList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({where: this.where});
    },
    /* 重置搜索 */
    reset() {
      this.where = {};
      this.reload();
    },
    /* 显示编辑 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({lock: true});
      this.$http.delete('/commercialacquisitionreport/delete', {id: row.id}).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      this.$confirm('确定要删除选中的收购报表吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.delete('/commercialacquisitionreport/delete', {id: this.selection.map(d => d.id)}).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },
  }
}
</script>

<style scoped>
</style>
